<template>
    <div class="e-form">
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.fixedAssetsCheck" label-width="200px" :rules="rules">
                <el-row>
                    <!-- <el-col :span="12">
                        <el-form-item label="盘点编号：">
                            <span>{{ formData.fixedAssetsCheck.billNo }}</span>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="12">
                        <el-form-item label="年度：" prop="year">
                            <el-select
                                v-model="formData.fixedAssetsCheck.year"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="(
                                        item
                                    ) in budgetYearList"
                                    :key="item"
                                    :label="item"
                                    :value="item"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                            <span>{{ formData.fixedAssetsCheck.orgName }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                     <el-col :span="12">
                        <el-form-item label="使用（保管）人：" prop="preserver">
                            <el-input v-model="formData.fixedAssetsCheck.preserver"  readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectHandling" ></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                            <span>{{ statusLable(formData.fixedAssetsCheck.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                >保存</el-button
            >
            <el-button size="small" @click="handleClose">取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/dailyManagement.js'
import { selectName } from '@/api/equip/popupCallback.js'
import { mapState } from 'vuex'
export default {
    data () {
        return {
            formData: {
                //基础信息
                'fixedAssetsCheck': {
                    'billNo': '',
                    'fixedAssetsCheckId': '',
                    'gmtCreate': '',
                    'gmtModified': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDesciption': '',
                    'nullifyReason': '',
                    'orgId': '',
                    'orgName': '',
                    'preserver': '',
                    'preserverId': '',
                    'state': 0,
                    'year': ''
                },
            },
            budgetYearList: [], //年度
            rules: {
                year: [
                    { required: true, message: '请选择年度', trigger: 'blur' },
                ],
                preserver: [
                    { required: true, message: '请选择使用（保管）人', trigger: ['blur', 'change']  },
                ],
            },
        }
    },
    components: {
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            workStatus: state=>state.equip.equipData.workStatus,
            assessmentLevel: state=>state.equip.equipData.assessmentLevel,
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
    },
    created () {
        this.init()
    },
    mounted () {
    },
    methods: {
        init () {
            this.formData.fixedAssetsCheck.orgName = this.userInfo.orgInfo.orgName
            this.formData.fixedAssetsCheck.orgId = this.userInfo.orgInfo.orgId
            const date = new Date()
            const year = date.getFullYear()
            this.formData.fixedAssetsCheck.year = new String(year)
            for(let i = 5;i--;i >= 0) {
                this.budgetYearList.push(year - i)
            }
        },
        //选择经办人 (选择本机构用户)
        selectHandling () {
            selectName().then(res=>{
                this.formData.fixedAssetsCheck.preserver = res[0].userName
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if (valid) {
                    request.assetsInventoryAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/assetsInventoryEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                } else {
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/assetsInventoryList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
